import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'

import viteCompression from 'vite-plugin-compression'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

// https://vitejs.dev/config/
export default defineConfig({
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src/')
    }
  },
  server: {
    port: 3001,
    open: true,
    proxy: {
      // 选项写法
      '/api': {
        target: 'http://console.yufeijiuke.com', // 所要代理的目标地址
        changeOrigin: true, // true/false, Default: false - changes the origin of the host header to the target URL
        rewrite: path => {
          return path.replace(/^\/api/, '/api')
        } // 重写传过来的path路径，比如 `/api/index/1?id=10&name=zs`（注意:path路径最前面有斜杠（/），因此，正则匹配的时候不要忘了是斜杠（/）开头的；选项的 key 也是斜杠（/）开头的）
      }
    }
  },
  build: {
    sourcemap: false,
    minify: 'terser',
    assetsInlineLimit: 10240,
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true
      }
    },
    rollupOptions: {
      plugins: [],
      output: {
        manualChunks (id) {
          if (id.includes('node_modules')) {
            if (id.includes('ant-design-vue')) {
              return 'antDesignVue'
            } else if (id.includes('moment')) {
              return 'moment'
            } else {
              return 'lib'
            }
          }
        }
      }
    }
  },
  plugins: [
    vue(),
    createSvgIconsPlugin({
      // 指定需要缓存的图标文件夹
      iconDirs: [resolve(process.cwd(), 'src/assets/svg')],
      // 指定symbolId格式
      symbolId: 'icon-[dir]-[name]'
    }),
    // gzip压缩
    viteCompression({
      verbose: true,
      disable: false,
      threshold: 5120,
      algorithm: 'gzip',
      ext: '.gz'
    })

  ],
  css: {
    preprocessorOptions: {
      less: {
        // additionalData: `$injectedColor: orange;`
        javascriptEnabled: true
      }
    }
  }
})
